package com.happy.mycar;

import android.content.SharedPreferences;

public class ESP_Config {
    public ESP_Config() {
    }

    public int ESP32_RSSI_POS_THRESHOLD = -75; // 替换为你希望的 RSSI 阈值
    public int ESP32_RSSI_NEG_THRESHOLD = -85; // 替换为你希望的 RSSI 阈值
    public boolean AUTO_LOCK = true; // 是否自动锁车
    public boolean AUTO_UNLOCK = true; // 是否自动解锁
    public boolean IS_LOCK = false; // 是否锁车

    public short LOCK_KEY = 0; // 关锁键
    public short UNLOCK_KEY = 0; // 开锁键
    public short TRUNK_KEY = 0; // 开尾箱键

    public short WINDOW_KEY = 0; // 开窗键
    public boolean IS_Need_SAVE = false; // 是否保存配置

    SharedPreferences sharedPreferences;

    public void LoadConfig(SharedPreferences sharedPreferences) {
        this.sharedPreferences = sharedPreferences;
        ESP32_RSSI_POS_THRESHOLD = sharedPreferences.getInt("ESP32_RSSI_POS_THRESHOLD", -75);
        ESP32_RSSI_NEG_THRESHOLD = sharedPreferences.getInt("ESP32_RSSI_NEG_THRESHOLD", -85);
        AUTO_LOCK = sharedPreferences.getBoolean("AUTO_LOCK", true);
        AUTO_UNLOCK = sharedPreferences.getBoolean("AUTO_UNLOCK", true);
        LOCK_KEY = (short) sharedPreferences.getInt("LOCK_KEY", 0);
        UNLOCK_KEY = (short) sharedPreferences.getInt("UNLOCK_KEY", 1);
        TRUNK_KEY = (short) sharedPreferences.getInt("TRUNK_KEY", 2);
        WINDOW_KEY= (short) sharedPreferences.getInt("WINDOW_KEY", 3);
    }

    public void SaveConfig() {
        if (IS_Need_SAVE && sharedPreferences != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("ESP32_RSSI_POS_THRESHOLD", ESP32_RSSI_POS_THRESHOLD);
            editor.putInt("ESP32_RSSI_NEG_THRESHOLD", ESP32_RSSI_NEG_THRESHOLD);
            editor.putInt("LOCK_KEY", LOCK_KEY);
            editor.putInt("UNLOCK_KEY", UNLOCK_KEY);
            editor.putInt("TRUNK_KEY", TRUNK_KEY);
            editor.putBoolean("AUTO_LOCK", AUTO_LOCK);
            editor.putBoolean("AUTO_UNLOCK", AUTO_UNLOCK);
            editor.putInt("WINDOW_KEY", WINDOW_KEY);
            editor.apply();
            IS_Need_SAVE = false;
        }
    }
}
